package com.utils
{
	/**
	 * 地图编辑器
	 * @author Lufu
	 */	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.net.FileReference;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	public class CustomMapUtil extends MovieClip
	{
		private var mapContain:Sprite;//地图容器
		private var uiContain:Sprite;//UI容器
		
		private var resultListBtn:Sprite;
		private var createMapBtn:Sprite;
		private var clearMapBtn:Sprite;
		private var saveBtn:Sprite;
		
		private var grid:Grid;//格子
		
		private var mapGridList:Array;//地图格子数组
		
		private var mapIndex:int;//地图数组序列
		private var fileReference:FileReference;
		
		private var offsetWidth:int = 30;
		private var offsetHeight:int = 20;
		private var offsetX:int = 0;
		
		private var mapRows:uint = 10;
		private var mapCols:uint = 10;
		private var gridSize:uint = 60;
		
		private var inputRowText:TextField;
		private var inputColText:TextField;
		private var inputSizeText:TextField;
		
		public function CustomMapUtil(gridSize:uint = 60,mapRows:uint = 10,mapCols:uint = 10,offsetX:int = 0)
		{
			this.mapRows = mapRows;
			this.mapCols = mapCols;
			this.gridSize = gridSize;
			this.offsetX = offsetX;
		}
		
		/**
		 * 初始化地图数据 
		 */		
		public function init():void{
			initVariable();
			initMap();
			initUI();
			initListens();
		}
		
		/**设置属性*/		
		private function initVariable():void
		{
			fileReference = new FileReference();
			mapContain = new Sprite();
			mapContain.y = 46;
			addChild(mapContain);
			uiContain = new Sprite();
			addChild(uiContain);
		}
		
		/**初始化背景*/		
		private function initMap():void
		{
			drawMapBg();
			
			mapGridList = [];
			for (var i:int = 0; i < mapRows; i++) 
			{
				var tempList1D:Array = [];
				mapGridList.push(tempList1D);
				for (var j:int = 0; j < mapCols; j++) 
				{
					grid = new Grid();
					grid.createGrid();
					mapContain.addChild(grid);
					grid.width = grid.height = gridSize;
					grid.x = j * gridSize;
					grid.y = i * gridSize;
					grid.name = 'gride_' + i + '_' +  j;
					tempList1D.push(grid);
				}
			}
		}
		
		/**绘制地图背景*/		
		private function drawMapBg():void{
			mapContain.graphics.clear();
			mapContain.graphics.lineStyle(2,0xFF0000);
			mapContain.graphics.beginFill(0,.7);
			mapContain.graphics.drawRect(-2,-2,mapCols*gridSize + 3,mapRows*gridSize + 3);
			mapContain.graphics.endFill();
		}
		
		/**添加UI*/		
		private function initUI():void
		{
			var txtSize:TextField = newTextField(uiContain,'格子尺寸:',0,true,offsetWidth*0+offsetX,0);
			inputSizeText = newTextField(uiContain,String(gridSize),0,false,offsetWidth * 2.5 + offsetX,0,50,0,0xFFFFFF,'0-9',3);
			
			var txtRow:TextField = newTextField(uiContain,'行:',0,true,offsetWidth*4.5 + offsetX,0);
			inputRowText = newTextField(uiContain,String(mapRows),0,false,offsetWidth*5.5 + offsetX,0,50,0,0xFFFFFF,'0-9',3);
			
			var txtCol:TextField = newTextField(uiContain,'列:',0,true,offsetWidth*4.5 + offsetX,offsetHeight);
			inputColText = newTextField(uiContain,String(mapCols),0,false,offsetWidth*5.5 + offsetX,offsetHeight,50,0,0xFFFFFF,'0-9',3);
			
			clearMapBtn = newBtn(uiContain,offsetWidth*5.5 + 70 + offsetX,offsetHeight/3,70,30,'清除地图',0,true);
			uiContain.addChild(clearMapBtn);
			createMapBtn = newBtn(uiContain,offsetWidth*5.5 + 70 + offsetX,offsetHeight/3,70,30,'创建地图',0,true);
			uiContain.addChild(createMapBtn);
			resultListBtn = newBtn(uiContain,offsetWidth*5.5 + 70*2 + 10 + offsetX,offsetHeight/3,70,30,'打印数据',0,true);
			uiContain.addChild(resultListBtn);
			saveBtn = newBtn(uiContain,offsetWidth*5.5 + 70*3 + 20 + offsetX,offsetHeight/3,70,30,'保存文本',0,true);
			uiContain.addChild(saveBtn);
			
			saveBtn.visible = clearMapBtn.visible;
			resultListBtn.visible = clearMapBtn.visible;
			createMapBtn.visible = !clearMapBtn.visible;
		}
		
		/**添加侦听*/		
		private function initListens():void
		{
			resultListBtn.addEventListener(MouseEvent.CLICK,onGetMapDataHandler);
			saveBtn.addEventListener(MouseEvent.CLICK,onGetMapDataHandler);
			
			createMapBtn.addEventListener(MouseEvent.CLICK,onChangeMapShow);
			clearMapBtn.addEventListener(MouseEvent.CLICK,onChangeMapShow);
		}
		
		/**刷新视图*/		
		private function onChangeMapShow(e:MouseEvent=null):void
		{
			clearMapBtn.visible = !clearMapBtn.visible;
			createMapBtn.visible = !createMapBtn.visible;
			resultListBtn.visible = !resultListBtn.visible;
			saveBtn.visible = !saveBtn.visible;
			
			mapRows = int(inputRowText.text);
			mapCols = int(inputColText.text);
			gridSize = int(inputSizeText.text);
			
			if(e && e.currentTarget == createMapBtn){
				initMap();
				return;
			}
			
			for (var i:int = 0; i < mapGridList.length; i++) 
			{
				for (var j:int = 0; j < mapGridList[i].length; j++) 
				{
					var grid:Grid = mapGridList[i][j];
					grid.dispose();
					mapContain.removeChild(grid);
					grid = null;
				}
			}
			mapGridList = [];
			
			while(mapContain.numChildren > 0)removeChildAt(0);
		}
		
		/**获取地图数据*/		
		private function onGetMapDataHandler(e:MouseEvent = null):void
		{
			mapIndex ++;
			var resultStr:String = '';
			var listStr:Array = mapList();
			for (var i:int = 0; i < listStr.length; i++) 
			{
				var sign:String = '';
				for (var j:int = 0; j < listStr[i].length; j++) 
				{
					if(i==0 && j== 0){
						sign += '[\n[' + listStr[i][j];
					}else if(i !=0 && j == 0){
						sign += '[' + listStr[i][j];
					}else if(i !=listStr.length -1 && j == listStr[i].length -1){
						sign +=  ',' + listStr[i][j] + '],';
					}else if(i ==listStr.length -1 && j == listStr[i].length -1){
						sign += ',' + listStr[i][j] + ']\n]';
					}else{
						sign += ',' + listStr[i][j];
					}
				}
				resultStr += sign + '\n';
			}
			resultStr = 'var mapList'+ mapIndex + ':Array = \n' + resultStr;
			if(e && e.target == saveBtn){
				fileReference.save(resultStr,"地图数组" + mapIndex + ".doc");
			}else{
				trace('>>>>>>>地图数组' + mapIndex +'<<<<<<')
				trace(resultStr);
			}
		}
		
		/**地图二维数组*/		
		public function mapList():Array
		{
			var mapList:Array = [];
			for (var i:int = 0; i < mapRows; i++) 
			{
				var tempList1D:Array = [];
				mapList.push(tempList1D);
				for (var j:int = 0; j < mapCols; j++) 
				{
					tempList1D.push(mapGridList[i][j].index);
				}
			}
			return mapList;
		}
		
		/**创建按钮*/		
		private function newBtn(contain:Sprite,btnX:int = 0,btnY:int = 0,btnWidth:int = 70,btnHeight:int = 30,name:String = '确定',txtColor:int = 0,dynamic:Boolean = true):Sprite{
			var sp:Sprite = new Sprite();
			sp.graphics.lineStyle(1,0xFF9900);
			sp.graphics.beginFill(0xFFFFFF,.8);
			sp.graphics.drawRoundRect(0,0,btnWidth,btnHeight,5,5);
			sp.graphics.endFill();
			var txt:TextField = newTextField(sp,name,txtColor,dynamic);
			contain.addChild(sp);
			sp.buttonMode = true;
			sp.x = btnX;
			sp.y = btnY;
			sp.name = name;
			return sp;
		}
		
		/**添加文本*/	
		private function newTextField(targetContain:Sprite,str:String,txtColor:int,
									  dynamic:Boolean = true,x:int=9999,y:int=9999,
									  txtWidth:int = 0,borderColor:int = -1,backgrondColor:int = -1,
									  restrict:String = '0-9',maxChars:int = 2):TextField
		{
			var txt:TextField = new TextField();
			txt.type = dynamic ? TextFieldType.DYNAMIC : TextFieldType.INPUT;
			if(dynamic) txt.mouseEnabled = false;
			txt.textColor = txtColor;
			txt.text = str;
			if(!dynamic){
				txt.restrict = restrict;
				txt.maxChars = maxChars;
			}
			if(txtWidth != 0){
				txt.width = txtWidth;
			}else{
				txt.autoSize = TextFieldAutoSize.LEFT;
			}
			txt.height = 20;
			if(borderColor != -1){
				txt.border = true;
				txt.borderColor = borderColor;
			}
			if(backgrondColor != -1){
				txt.background = true;
				txt.backgroundColor = backgrondColor;
			}
			var format:TextFormat = new TextFormat();
			format.bold = true;
			format.size = 15;
			txt.setTextFormat(format);
			
			if(x == 9999 && y == 9999){
				txt.x = (targetContain.width- txt.width)/2;
				txt.y = (targetContain.height - txt.height)/2
			}else{
				txt.x = x;
				txt.y = y;
			}
			targetContain.addChild(txt);
			return txt;
		}
		
		/**数据清除*/		
		public function dispose():void{
			if(numChildren == 0) return;
			resultListBtn.removeEventListener(MouseEvent.CLICK,onGetMapDataHandler);
			saveBtn.removeEventListener(MouseEvent.CLICK,onGetMapDataHandler);
			
			createMapBtn.removeEventListener(MouseEvent.CLICK,onChangeMapShow);
			clearMapBtn.removeEventListener(MouseEvent.CLICK,onChangeMapShow); 
			
			onChangeMapShow();
			
			mapContain.graphics.clear();
			
			fileReference = null;
			
			while(mapContain.numChildren > 0) mapContain.removeChildAt(0);
			removeChild(mapContain);
			mapContain = null;
			
			while(uiContain.numChildren > 0) uiContain.removeChildAt(0);
			removeChild(uiContain);
			uiContain = null;
			
			while(this.numChildren > 0) removeChildAt(0);
			
			resultListBtn = null;
			createMapBtn = null;
			clearMapBtn = null;
			saveBtn = null;
			
			inputRowText = null;
			inputColText = null;
			inputSizeText = null;
		}
		

	}
}




import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

class Grid extends Sprite{
	private var colorList:Array = [0,0xFF0000,0x006600,0x0000FF,0xFF9900,0x00FFFF,0xFF00FF,0x996600,0x669900,0x6633FF,0x0099FF];
	
	public var contain:Sprite;
	private var clickContain:Sprite;
	private var plusContain:Sprite;
	private var minusContain:Sprite;
	private var txtGridIndex:TextField;
	private var _index:int = 0;
	private var _name:String;
	private var _row:int;
	private var _col:int;
	
	public function Grid(){
		init();
	}
	
	private function init():void{
		initContain();
		
		initListens();
		
		addPlusMinus();
	}
	
	private function initContain():void
	{
		contain = new Sprite();
		this.addChild(contain);
		
		txtGridIndex = new TextField;
		
		clickContain = new Sprite();
		this.addChild(clickContain);
		clickContain.alpha = 0;
		clickContain.x = 19;
		clickContain.y = 1;
	}
	
	private function initListens():void
	{
		clickContain.addEventListener(MouseEvent.MOUSE_OVER,onMouseHandler);
		clickContain.addEventListener(MouseEvent.MOUSE_OUT,onMouseHandler);
		contain.addEventListener(MouseEvent.CLICK,onClickThisHandler);
	}
	
	private function onClickThisHandler(e:MouseEvent):void{
		trace('row:' + row,'col:' + col);
	}
	
	/**
	 * 移除视图时,调用
	 */
	public function dispose():void{
		
		clickContain.removeEventListener(MouseEvent.MOUSE_OVER,onMouseHandler);
		clickContain.removeEventListener(MouseEvent.MOUSE_OUT,onMouseHandler);
		
		contain.removeEventListener(MouseEvent.CLICK,onClickThisHandler);
		
		plusContain.removeEventListener(MouseEvent.CLICK,onClickHandler);
		minusContain.removeEventListener(MouseEvent.CLICK,onClickHandler);
		
		
		while(plusContain.numChildren > 0) plusContain.removeChildAt(0);
		plusContain = null;
		while(minusContain.numChildren > 0) minusContain.removeChildAt(0);
		minusContain = null;
		while(clickContain.numChildren > 0) clickContain.removeChildAt(0);
		clickContain = null;
		while(contain.numChildren > 0) contain.removeChildAt(0);
		contain = null;
		while(numChildren > 0) removeChildAt(0);
	}
	
	private function onMouseHandler(e:MouseEvent):void
	{
		e.type == MouseEvent.MOUSE_OVER ? clickContain.alpha = 1 : clickContain.alpha = 0;
	}
	
	/**绘制'+','-'容器及父容器*/	
	private function addPlusMinus():void{
		clickContain.graphics.beginFill(0xFF0000,0);
		clickContain.graphics.drawRect(0,0,10,28);
		clickContain.graphics.endFill();
		
		plusContain = new Sprite();
		clickContain.addChild(plusContain);
		plusContain.buttonMode = true;
		setPlusMinus(plusContain,"+");
		
		minusContain = new Sprite();
		clickContain.addChild(minusContain);
		minusContain.y = 18;
		minusContain.buttonMode = true;
		setPlusMinus(minusContain,"-");
		
		if(!plusContain.hasEventListener(MouseEvent.CLICK)){
			plusContain.addEventListener(MouseEvent.CLICK,onClickHandler);
			minusContain.addEventListener(MouseEvent.CLICK,onClickHandler);
		}
	}
	
	/**根据被点击的对象刷新索引*/	
	private function onClickHandler(e:MouseEvent):void
	{
		e.currentTarget == plusContain ? index++ : index--;
		if(index <= 0 ) index = 0;
	}
	
	/**
	 * 添加加减 
	 */	
	private function setPlusMinus(contain:Sprite,value:String):void{
		contain.graphics.clear();
		contain.graphics.beginFill(0xFFFFFF,.7);
		contain.graphics.drawRect(0,0,10,10);
		contain.graphics.endFill();
		if(value == "+"){
			contain.graphics.beginFill(0);
			contain.graphics.drawRect(3.5,1,3,8);
			contain.graphics.endFill();
		}
		contain.graphics.beginFill(0);
		contain.graphics.drawRect(1,3.5,8,3);
		contain.graphics.endFill();
	}
	
	/**
	 * 创建格子视图
	 * @param borderColor
	 * @param thickness
	 * @param txtColor
	 */	
	public function createGrid(borderColor:int = 0xDDDDDD,thickness:int = 1,txtColor:int = 0xFFFFFF):void{
		drawBox(thickness,borderColor);
		addTxt(txtColor);
	}
	
	/**
	 * 添加文本
	 * @param txtColor
	 */	
	private function addTxt(txtColor:int):void
	{
		txtGridIndex.mouseEnabled = false;
		txtGridIndex.autoSize = TextFieldAutoSize.LEFT;
		txtGridIndex.textColor = txtColor;
		txtGridIndex.text = index + '';
		//txtGridIndex.background = true;
		//txtGridIndex.backgroundColor = 0x00ffff;
		var format:TextFormat = new TextFormat();
		format.bold = true;
		format.size = 15;
		txtGridIndex.setTextFormat(format);
		
		txtGridIndex.x = (contain.width- txtGridIndex.width)/2;
		txtGridIndex.y = (contain.height - txtGridIndex.height)/2;
		addChild(txtGridIndex);
	}
	
	/**
	 * 绘制背景 
	 * @param thickness 边框宽度
	 * @param borderColor 边框颜色
	 */	
	private function drawBox(thickness:int,borderColor:int):void
	{
		contain.graphics.clear();
		contain.graphics.lineStyle(thickness,borderColor);
		contain.graphics.beginFill(colorList[index % 11],1);
		contain.graphics.drawRect(0,0,30,30);
		contain.graphics.endFill();
	}	
	
	public function get index():int{return _index;}
	
	/**
	 * 改变格子值同时,刷新视图 
	 * @param value 格子显示的数字
	 */	
	public function set index(value:int):void
	{
		_index = value;
		createGrid();
	}
	
	override public function get name():String{return _name;}
	
	/**重写name属性,同时设置其他对象name属性*/
	override public function set name(value:String):void
	{
		_name = value;
		setAboutPropertyInfo();
	}
	
	/**设置相关属性 */	
	private function setAboutPropertyInfo():void{
		contain.name = _name;
		clickContain.name = _name;
		plusContain.name = 'PLUS';
		minusContain.name = 'MINUS';
		_row = int(_name.split('_')[1]);
		_col = int(_name.split('_')[2]);
	}
	
	public function get col():int{return _col;}
	
	public function get row():int{return _row;}
	
	
	
}